//获取节点
const moveEle = document.querySelector('.move')
const moveRangeEle = document.querySelector('.moveRange')
const bigBox = document.querySelector('.bigBox')
const bigPic = document.querySelector('.bigBox>img')

function mouseMove() {
    //获取元素p的尺寸
    moveRangeEle.onmousemove = function (e) {
        //获取事件对象
        e = e || window.event
        //获取光标位置,并把p元素的光标点设置成中心
        let x = e.offsetX - moveEle.offsetWidth / 2
        let y = e.offsetY - moveEle.offsetHeight / 2
        //边界判断
        //左边界
        if (x < 0) {
            x = 0
        }
        //右边界
        if (x > moveRangeEle.offsetWidth - moveEle.offsetWidth) {
            x = moveRangeEle.offsetWidth - moveEle.offsetWidth
        }
        // 上边界
        if (y < 0) {
            y = 0
        }
        // 下边界
        if (y > moveRangeEle.offsetHeight - moveEle.offsetHeight) {
            y = moveRangeEle.offsetHeight - moveEle.offsetHeight
        }

        //获取移动的比例
        /**
         遮罩层移动距离        遮罩层
         ------------   =  ------------
         背景图片移动距离      放大镜

          背景图片移动距离 =  遮罩层移动距离*放大镜/遮罩层
       **/
        let moveX = x * bigBox.offsetWidth / moveEle.offsetWidth
        let moveY = y * bigBox.offsetHeight / moveEle.offsetHeight

        moveEle.style.top = y + 'px'
        moveEle.style.left = x + 'px'

        bigPic.style.top = -moveY + 'px'
        bigPic.style.left = -moveX + 'px'


    }
    //鼠标移入出现
    moveRangeEle.onmouseover = function () {
        moveEle.style.display = 'block'
        //计算背景图比例
        /*计算背景图比例
           遮罩层mask            放大镜bigGlass
          ---------------   =   ------------------
           显示图片showbox        背景图bigPicBox
         
           背景图片bigpicbox = 放大镜bigGlass*显示图片showbox/遮罩层mask
        */
        bigPic.style.width = bigBox.offsetWidth * moveRangeEle.offsetWidth / moveEle.offsetWidth + 'px'
        bigPic.style.height = bigBox.offsetHeight * moveRangeEle.offsetHeight / moveEle.offsetHeight + 'px'
    }
    //鼠标移出隐藏
    moveRangeEle.onmouseout = function () {
        moveEle.style.display = 'none'
    }
}

//构造函数
function changePic(olliEle, ulliEle) {
    this.olliEle = olliEle
    this.ulliEle = ulliEle
}
changePic.prototype.change = function () {
    //遍历li
    for (let i = 0; i < this.ulliEle.length; i++) {
        //获取当前对象
        let _this = this
        //清除所有样式
        this.ulliEle[i].onclick = function () {
            _this.clear()
            _this.olliEle[i].classList.add('active')
            _this.ulliEle[i].classList.add('addBorder')
            bigPic.setAttribute('src', `./image/show_${i+1}.jpg`)
        }

    }
}
changePic.prototype.clear = function () {
    for (let i = 0; i < this.olliEle.length; i++) {

        this.olliEle[i].classList.remove('active')
        this.ulliEle[i].classList.remove('addBorder')

    }
}
const olliEle = document.querySelectorAll('ol>li')
const ulliEle = document.querySelectorAll('ul>li')
let changeObj = new changePic(olliEle, ulliEle)
changeObj.change()

mouseMove()